package com.marry.intelli.plugin.component;

import javax.swing.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 * @author boyfighting@qq.com
 * @desc
 * @date 2019/4/28
 */
public class MJTextField extends JTextField implements MouseListener {

    private static final String DEFAULT_PLACEHOLDER = "namespace.id";

    /**
     * 弹出菜单
     */
    private JPopupMenu pop = null;

    /**
     * 三个菜单功能键
     */
    private JMenuItem copy = null, paste = null, cut = null;

    public MJTextField() {
        super();
        init();
    }

    private void init() {
        this.addMouseListener(this);
        pop = new JPopupMenu();
        pop.add(copy = new JMenuItem("复制"));
        pop.add(paste = new JMenuItem("粘贴"));
        pop.add(cut = new JMenuItem("剪切"));
        copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
        paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
        cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
        copy.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                action(e);
            }
        });

        paste.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                action(e);
            }
        });

        cut.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                action(e);
            }
        });
        this.add(pop);

    }

    private void action(ActionEvent e) {
        String str = e.getActionCommand();
        System.out.println("str:"+str);
        if (str.equals(copy.getText())) {
            this.copy();
        } else if (str.equals(paste.getText())) {
            this.paste();
        } else if (str.equals(cut.getText())) {
            this.cut();
        }
    }


    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
            if (DEFAULT_PLACEHOLDER.equals(getText())) {
                setText("");
            }
            copy.setEnabled(isCanCopy());
            paste.setEnabled(isClipboardString());
            cut.setEnabled(isCanCopy());
            pop.show(this, e.getX(), e.getY());
            
        }

    }

    private boolean isClipboardString() {
        boolean b = false;
        Clipboard clipboard = this.getToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(this);
        try {
            if (contents.getTransferData(DataFlavor.stringFlavor) instanceof String) {
                b = true;
            }
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }

    private boolean isCanCopy() {
        boolean b = false;
        int start = this.getSelectionStart();
        int end = this.getSelectionEnd();
        if (start != end) {
            return true;
        }
        return b;
    }
    
    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
